using System.IO;
using System.Collections.Generic;
using UnityEngine;
namespace Framework
{
    public class AssetProcessorBase : IAssetProcessor
    {
        private readonly Dictionary<string, MemoryStream> _decryptMap = new Dictionary<string, MemoryStream>();
        private readonly Dictionary<string, byte[]> _decryptFileMap = new Dictionary<string, byte[]> ();
        public virtual void InitAssetsInfo(AssetsInfo value)
        {
            int size = 0;
            foreach (var item in value.Bundles)
            {
                if (item.Encrypt > 0)
                {
                    if (item.Size > size)
                    {
                        size = item.Size;
                    }
                }
            }
            if (size < 1) return;
        }
        public virtual void OnLoadBundle(AssetBundle value, string path)
        {
            // FrameworkLog.LogError("load " + path);
        }

        public virtual void ProcessAsset(object target, string path)
        {

        }
        public virtual bool DontUnloadBundle(string path)
        {
            return false;
        }

        public virtual void OnUnloadBundle(string path)
        {
            // FrameworkLog.LogError("unload " + path);
            if (_decryptMap.TryGetValue(path, out MemoryStream data))
            {
                data.Close();
                data.Dispose();
                _decryptMap.Remove(path);
            }
            if (_decryptFileMap.TryGetValue(path, out byte[] fileData))
            {
                Utils.File.ReleaseByteBuffer(fileData);
                _decryptFileMap.Remove(path);
            }
        }
        public virtual MemoryStream GetFile(string path)
        {
            if (_decryptMap.TryGetValue(path, out MemoryStream data))
            {
                return data;
            }
            if (_decryptFileMap.TryGetValue(path, out byte[] fileData))
            {
                MemoryStream ms = new(fileData);
                _decryptMap.Add(path, ms);
                return ms;
            }
            return null;
        }
        public virtual MemoryStream ProcessFile(string path, byte[] dataBuffer, int length, int offset)
        {
            if (_decryptMap.TryGetValue(path, out MemoryStream data))
            {
                return data;
            }
            if (!_decryptFileMap.TryGetValue(path, out byte[] fileData))
            {
                fileData = Utils.File.GetByteBuffer(length);
                for (int i = 0; i < length; i++)
                {
                    fileData[i] = dataBuffer[i + offset];
                }
                _decryptFileMap.Add(path, fileData);
                Framework.Utils.Encrypt.XorSelf(fileData, GetEncryptData());
            }
            MemoryStream ms = new(fileData, 0, length);
            _decryptMap.Add(path, ms);
            return ms;
        }
        protected virtual byte[] GetEncryptData()
        {
            return null;
        }

        public void ProcessFile(byte[] dataBuffer)
        {
            Framework.Utils.Encrypt.XorSelf(dataBuffer, GetEncryptData());
        }
    }
}